package com.tjsinfo.tjsoa.documentationflow.dangweishouwen.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.WritableFont;
import jxl.write.WriteException;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.vo.TDangweishouwenNeirong;
import com.tjsinfo.tjsoa.system.dao.ISystemSpringDao;
import java.net.URLDecoder;

public class DengJiBuAction extends DispatchAction{
	private ISystemSpringDao springDao;
	

	public ISystemSpringDao getSpringDao() {
		return springDao;
	}


	public void setSpringDao(ISystemSpringDao springDao) {
		this.springDao = springDao;
	}


	public ActionForward dengjibuSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException, WriteException {
		jxl.write.WritableWorkbook wwb = null;
		try {
			String beginNO=request.getParameter("beginNO");
			String endNO=request.getParameter("endNO");
			String yearNo=request.getParameter("yearNO");
			String doctype=request.getParameter("doctype");
			doctype=URLDecoder.decode(URLDecoder.decode(doctype,"UTF-8"),"UTF-8");  
			
			String whereSQL="";
			if (beginNO!=null && !"".equals(beginNO)) {
				whereSQL+="shouwenbianhao>="+beginNO+" and ";
			}
			if (endNO !=null && !"".equals(endNO)) {
				whereSQL+="shouwenbianhao<="+endNO+" and  ";
			}
			if (yearNo !=null && !"".equals(yearNo) && doctype !=null && !"".equals(doctype)) {
				whereSQL+="shouwentype='"+doctype+yearNo+"' and ";
			}
			
			
	//		List<TXingzhengshouwenNeirong> shouwenList=qbcDao.getCriteria(criteria);
			List<Map<String, String>> tempList = springDao.commonSelect(new String[]{
					"shouwenbianhao","shouwenriqi","laiwenzihao","laiwenjiguan","zhaiyou"},
					"SELECT shouwenbianhao, shouwenriqi, laiwenzihao, laiwenjiguan, zhaiyou FROM t_dangweishouwen_neirong where "
					+whereSQL+" flowid <> 0 order by shouwenbianhao");

			JSONArray array = new JSONArray();
			JSONObject object = new JSONObject();
			for (Map<String, String> map : tempList) { 
				JSONObject json = new JSONObject();
				json.put("shouwenbianhao",doctype+yearNo+"年"+map.get("shouwenbianhao")+"号");
				json.put("shouwenriqi", map.get("shouwenriqi").substring(0, 10));
				json.put("laiwenzihao", map.get("laiwenzihao"));
				json.put("laiwenjiguan", map.get("laiwenjiguan"));
				json.put("zhaiyou", map.get("zhaiyou"));
				array.put(json);
			}
			

			object.put("root", array);
			response.getWriter().print(object);
			
//			DetachedCriteria criteria = DetachedCriteria.forClass(TDangweishouwenNeirong.class);
//			if (beginNO!=null && !"".equals(beginNO)) {
//				criteria.add(Restrictions.ge("shouwenbianhao", beginNO));
//			}
//			if (endNO !=null && !"".equals(endNO)) {
//				criteria.add(Restrictions.le("shouwenbianhao", endNO));
//			}
//
//			criteria.addOrder(Order.asc("shouwenbianhao"));
//			
//			JSONArray array = new JSONArray();
//			JSONObject object = new JSONObject();
//			List<TDangweishouwenNeirong> shouwenList=qbcDao.getCriteria(criteria);
//			for (TDangweishouwenNeirong map : shouwenList) {
//				JSONObject json = new JSONObject();
//				json.put("shouwenbianhao", map.getShouwenbianhao());
//				json.put("shouwenriqi", map.getShouwenriqi());
//				json.put("laiwenzihao", map.getLaiwenzihao());
//				json.put("laiwenjiguan", map.getLaiwenjiguan());
//				json.put("zhaiyou", map.getZhaiyou());
//				array.put(json);
//			}
//
//			object.put("root", array);
//			response.getWriter().print(object);
//
//			String uuid=ToolKit.getUUID();
//			File f = new File("C:\\tempFile\\"+uuid+".xls");
//			wwb = Workbook.createWorkbook(f);
//			jxl.write.WritableSheet ws = wwb.createSheet("��ѯ���", 0);
//			ws.addCell(new jxl.write.Label(0,0, "���ı��"));
//			ws.addCell(new jxl.write.Label(1,0, "��������"));
//			ws.addCell(new jxl.write.Label(2,0, "�����ֺ�"));
//			ws.addCell(new jxl.write.Label(3,0, "���Ļ��"));
//			ws.addCell(new jxl.write.Label(4,0, "ժҪ"));
//			ws.addCell(new jxl.write.Label(5,0, "ǩ��"));
//			ws.addCell(new jxl.write.Label(6,0, "��ע"));
//			for (int i=0,j=1;i<shouwenList.size();i++,j++) {
//				ws.addCell(new jxl.write.Label(0,j, shouwenList.get(i).getShouwenbianhao()));
//				ws.addCell(new jxl.write.Label(1,j, shouwenList.get(i).getLaiwenriqi().toString().substring(0,10)));
//				ws.addCell(new jxl.write.Label(2,j, shouwenList.get(i).getLaiwenzihao()));
//				ws.addCell(new jxl.write.Label(3,j, shouwenList.get(i).getLaiwenjiguan()));
//				ws.addCell(new jxl.write.Label(4,j, shouwenList.get(i).getZhaiyou()));
//				ws.addCell(new jxl.write.Label(5,j, ""));
//				ws.addCell(new jxl.write.Label(6,j, ""));
//			}
//			
//
//			jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);
//			jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
//			wcfFC.setBackground(jxl.format.Colour.RED);
//			wwb.write();
//			wwb.close();
//			
//			request.setAttribute("uuid", uuid);
			return null;
			
		} catch (Exception e) {
		//	wwb.write();
		//	wwb.close();
			e.printStackTrace();
			return null;
		}
	}
}
